<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Python 笔记 | Aikikoの小窝</title><meta name="keywords" content="Python"><meta name="author" content="AikikoXHJ"><meta name="copyright" content="AikikoXHJ"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="这篇笔记是看《Python编程从入门到实践》的基础部分时记录的，主要是一些常见的知识点，奈何自己脑子不好使，只能烂笔头弥补一下，可以当做自己的知识点查询工具，后续应该还会继续更新，会不会另起一篇就好说啦~总之，如果有错误的地方，还请指出，谢谢！！！ 字符操作string.title()：将字符串的第一个字符变成大写 string.upper()：将字符串全部变成大写 string.lower()：">
<meta property="og:type" content="article">
<meta property="og:title" content="Python 笔记">
<meta property="og:url" content="http://example.com/2021/06/16/PythonNotion/index.html">
<meta property="og:site_name" content="Aikikoの小窝">
<meta property="og:description" content="这篇笔记是看《Python编程从入门到实践》的基础部分时记录的，主要是一些常见的知识点，奈何自己脑子不好使，只能烂笔头弥补一下，可以当做自己的知识点查询工具，后续应该还会继续更新，会不会另起一篇就好说啦~总之，如果有错误的地方，还请指出，谢谢！！！ 字符操作string.title()：将字符串的第一个字符变成大写 string.upper()：将字符串全部变成大写 string.lower()：">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/background.jpg">
<meta property="article:published_time" content="2021-06-16T06:57:52.000Z">
<meta property="article:modified_time" content="2021-06-16T15:56:01.496Z">
<meta property="article:author" content="AikikoXHJ">
<meta property="article:tag" content="Python">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/background.jpg"><link rel="shortcut icon" href="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/XHJ.jpg"><link rel="canonical" href="http://example.com/2021/06/16/PythonNotion/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Python 笔记',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-06-16 23:56:01'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><z id="web_bg"></z><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/XHJ.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">2</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">2</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/background.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Aikikoの小窝</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Python 笔记</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-06-16T06:57:52.000Z" title="发表于 2021-06-16 14:57:52">2021-06-16</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-06-16T15:56:01.496Z" title="更新于 2021-06-16 23:56:01">2021-06-16</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Python 笔记"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>这篇笔记是看《Python编程从入门到实践》的基础部分时记录的，主要是一些常见的知识点，奈何自己脑子不好使，只能烂笔头弥补一下，可以当做自己的知识点查询工具，后续应该还会继续更新，会不会另起一篇就好说啦~总之，如果有错误的地方，还请指出，谢谢！！！</p>
<h3 id="字符操作"><a href="#字符操作" class="headerlink" title="字符操作"></a>字符操作</h3><p>string.title()：将字符串的第一个字符变成大写</p>
<p>string.upper()：将字符串全部变成大写</p>
<p>string.lower()：将字符串全部变成小写</p>
<p>pring()：打印</p>
<p>Python使用加号（+）来合并字符串</p>
<p>制表符：\t —&gt; print(“\tpython”)</p>
<p>换行符：\n </p>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_02.png"></p>
<p>string.lstrip()：删除字符串前的空格</p>
<p>string.rstrip()：删除字符串后的空格</p>
<p>单引号间不能单独加一个单引号，会报错，Python不能识别</p>
<p>双引号间可以加一个单引号，不会报错，Python能识别</p>
<p>两个乘号表示乘方运算:3**3⇒27</p>
<p>空格不影响计算表达式的方式</p>
<p>浮点数运算结果包含的小数位数是不确定的</p>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_01.png"></p>
<p>str()：可以将非字符串表示为字符串的函数</p>
<p>python2中除法会将小数部分直接删除，保留整数</p>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_03.png"></p>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_04.png"></p>
<p>python3的除法得到的结果则是浮点型的</p>
<p>python使用井号（#）作为注释标识</p>
<p>import this：这段命令会调用出 Tim Peters 的Python之禅</p>
<p>python使用方括号（ [ ] ）表示列表，使用逗号分隔元素</p>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_05.png"></p>
<p>python访问列表的最后一个元素，可以使用索引 -1 ，以此类推</p>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_06.png"></p>
<p>添加新的元素在列表中：列表名.append( ‘元素’ )，会自动添加到末尾</p>
<p>插入元素到列表中：列表名.insert(位置，’元素’ )，插入位置及之后的元素全部向后自动移一位</p>
<p>删除列表中的元素：</p>
<ul>
<li>del 列表名 [ 索引 ] ，删除索引位置的元素，后面的元素会自动补上</li>
<li>使用 列表名.pop()可以将列表末尾的元素弹出（出栈操作）</li>
<li>根据值删除元素：列表名.remove( ‘ 值 ‘ )，该方法只能删除列表中的第一个相同值得元素</li>
</ul>
<p>列表名.sort()：自动对列表进行永久性的排序（升序）；</p>
<p>列表名.sort(reverse=True)：自动对列表进行永久的排序（降序）；</p>
<p>sorted(列表名)：自动对列表进行临时排序（升序）；</p>
<p>sorted(列表名，reverse=True)：自动对列表进行临时排序（降序）；</p>
<p>列表名.reverse()：自动进行永久性翻转列表元素的排序顺序，再次使用即可翻转回来；</p>
<p>len(列表名)：获取列表的长度；</p>
<p>遍历整个列表（类foreach）：</p>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_07.png"></p>
<p>Range(start，end，步长)：</p>
<ul>
<li><p>生成从start到（end-1）的整数列表，（左闭右开结构）；</p>
</li>
<li><p>start默认从零开始，每次增加步长的值，直到达到或大于end值时结束；</p>
</li>
<li><p>当start&gt;end时，如果start+步长&gt;start，则无返回值，反之则有；</p>
<p>  使用C#进行功能实现</p>
  <figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> List&lt;<span class="built_in">int</span>&gt; <span class="title">Range</span>(<span class="params"><span class="built_in">int</span> start,<span class="built_in">int</span> end,<span class="built_in">int</span> len</span>)</span> &#123;</span><br><span class="line">            List&lt;<span class="built_in">int</span>&gt; range = <span class="keyword">new</span> List&lt;<span class="built_in">int</span>&gt;();</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (start &lt; end)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">for</span> (<span class="built_in">int</span> i = start; i &lt; end; i += len)</span><br><span class="line">                &#123;</span><br><span class="line">                    range.Add(i);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span> (start &gt; end)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">for</span> (<span class="built_in">int</span> i = start; i &gt; end; i += len)</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="keyword">if</span> (i + len &lt; i)</span><br><span class="line">                    &#123;</span><br><span class="line">                        range.Add(i);</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">else</span></span><br><span class="line">                    &#123;</span><br><span class="line">                        <span class="keyword">break</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> &#123;</span><br><span class="line">                range.Add(<span class="number">0</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> range;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<p>min(列表名)：获取列表的最小值；</p>
<p>max(列表名)：获取列表的最大值；</p>
<p>sum(列表名)：获取列表值得总和；</p>
<p>列表解析：</p>
<ul>
<li>将for循环和创建新元素的代码合并成一行，并自动附加新的元素；</li>
<li>for循环后面没有冒号（ : ）</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">squares = [value**<span class="number">2</span> <span class="keyword">for</span> value <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>,<span class="number">11</span>)]</span><br><span class="line"><span class="built_in">print</span>(squares)</span><br></pre></td></tr></table></figure>

<p>处理列表的部分元素——Python称之为切片：</p>
<ul>
<li>列表名[start：end]：获取列表从索引start开始到索引（end-1）位置的元素</li>
<li>start默认从0开始，end默认为列表的长度</li>
<li>单写一边的时候，则代表从单写的一端与另一端默认之间的元素</li>
<li>可以写负数，负数的作用与概念为列表中负数的概念</li>
</ul>
<p>复制列表：</p>
<ul>
<li><p>使用省略头尾索引的切片</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">players=[<span class="string">&#x27;charles&#x27;</span>,<span class="string">&#x27;martina&#x27;</span>,<span class="string">&#x27;michael&#x27;</span>,<span class="string">&#x27;florence&#x27;</span>,<span class="string">&#x27;eli&#x27;</span>]</span><br><span class="line">otherPlayers=players[:]</span><br></pre></td></tr></table></figure></li>
<li><p>如果不使用切片，而是直接赋值，则无法得到两个列表</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">otherPlayers=players</span><br></pre></td></tr></table></figure></li>
<li><p>直接赋值的方式，不是将列表 players 的副本存储到 otherPlayers里，而是让Python将新变量 otherPlayers 关联到包含在 players 中的列表，因此这两个变量都是指向同一个列表</p>
</li>
</ul>
<p>元组：</p>
<ul>
<li><p>不可变的列表成为元组</p>
</li>
<li><p>使用圆括号（ ( ) ）进行标识</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dimension=(<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>)</span><br></pre></td></tr></table></figure></li>
<li><p>访问方式与列表相同，但是无法对元组的元素进行赋值</p>
</li>
<li><p>修改元组的方法即重定义一次元组的所有元素</p>
</li>
</ul>
<p>if语句：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">cars=[<span class="string">&#x27;audi&#x27;</span>,<span class="string">&#x27;bmw&#x27;</span>,<span class="string">&#x27;subaru&#x27;</span>,<span class="string">&#x27;toyota&#x27;</span>]</span><br><span class="line"><span class="keyword">for</span> car <span class="keyword">in</span> cars:</span><br><span class="line">    <span class="keyword">if</span> car==<span class="string">&#x27;bmw&#x27;</span>:</span><br><span class="line">        <span class="built_in">print</span>(car.upper())</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="built_in">print</span>(car.title())</span><br><span class="line"><span class="comment">#=========================</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">Audi</span></span><br><span class="line"><span class="string">BMW</span></span><br><span class="line"><span class="string">Subaru</span></span><br><span class="line"><span class="string">Toyota</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>Python检查是否相等时，区分大小写</li>
<li>使用 and 作为连接符（与），进行多个条件进行检查</li>
<li>使用 or 作为连接符（或），进行多个条件进行检查</li>
<li>使用 in 关键字，判断特定值是否已包含在列表中，返回一个bool值<ul>
<li>‘ 关键字 ‘ in 列表名</li>
</ul>
</li>
<li>使用 not in 关键字，判断特定值是否不包含在列表中，返回一个bool值</li>
</ul>
<p>if-else语句：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">age=<span class="number">19</span></span><br><span class="line"><span class="keyword">if</span> age&gt;=<span class="number">18</span>:</span><br><span class="line">		<span class="built_in">print</span>(<span class="string">&quot;True&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">		<span class="built_in">print</span>(<span class="string">&quot;false&quot;</span>)</span><br></pre></td></tr></table></figure>

<p>if-elif-else语句：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">age =<span class="number">12</span> </span><br><span class="line"><span class="keyword">if</span> age &lt; <span class="number">4</span>:</span><br><span class="line">		<span class="built_in">print</span>(<span class="string">&quot;幼儿&quot;</span>)</span><br><span class="line"><span class="keyword">elif</span> age &gt;<span class="number">18</span></span><br><span class="line">		<span class="built_in">print</span>(<span class="string">&quot;成年人&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">		<span class="built_in">print</span>(<span class="string">&quot;青少年&quot;</span>)</span><br></pre></td></tr></table></figure>

<p>如果只想执行一个代码块，就使用 if-elif-else 结构；如果要运行多个代码块，就使用一系列独立的 if 语句</p>
<h3 id="字典："><a href="#字典：" class="headerlink" title="字典："></a>字典：</h3><ul>
<li>使用花括号填装内容，使用冒号进行键—值对应，键用引号</li>
</ul>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_08.png"></p>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_09.png"></p>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_10.png"></p>
<ul>
<li>字典是一种动态结构，可以随时在其中添加键—值对</li>
<li>键—值对的排列顺序与添加顺序不同</li>
</ul>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_11.png"></p>
<ul>
<li><p>使用 del 关键字进行删除，必须指定字典名和要删除的键</p>
<ul>
<li>del 字典名[ ‘键’ ]</li>
</ul>
</li>
<li><p>遍历字典：</p>
<ul>
<li><p>items( )方法会返回一个键—值对列表：for 存键的变量名，存值得变量名 in 字典名.items( ):</p>
</li>
<li><p>keys( )方法会返回键：for 存键的变量名 in 字典名.keys( ):</p>
</li>
<li><p>遍历字典时，默认遍历所有的键，遍历键的时候可以不写keys( )方法，会隐式实现</p>
</li>
<li><p>values( )方法会返回一个值列表：for 存值得变量名 in 字典名.values( ):</p>
<ul>
<li><p>这个方法不会剔除重复项，可以使用集合（set）来剔除列表中的重复项</p>
</li>
<li><p>for 存值的变量名 in set（字典名.values( )）</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_12.png"></p>
</li>
</ul>
</li>
</ul>
</li>
<li><p>嵌套：将一系列字典存储在列表中，或将列表作为值存储在字典里</p>
<ul>
<li><p>字典列表：</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_13.png"></p>
</li>
<li><p>列表和字典的嵌套层级不应该太多</p>
</li>
<li><p>可以在字典中嵌套字典，但是这样做可能会导致代码很快复杂起来</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_14.png"></p>
</li>
</ul>
</li>
</ul>
<h3 id="用户输入与求模运算"><a href="#用户输入与求模运算" class="headerlink" title="用户输入与求模运算"></a>用户输入与求模运算</h3><p>input ( ) 输入:</p>
<ul>
<li><p>函数 input ( ) 会让程序暂停运行，等待用户输入一些文本，获取用户的输入后，Python会将其存储在一个变量中</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_15.png"></p>
</li>
</ul>
<p>int ( ) 输入：</p>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_16.png"></p>
<p>求模运算符：%</p>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_17.png"></p>
<p>Python3和2中用户输入的区别：</p>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_18.png"></p>
<h3 id="While循环："><a href="#While循环：" class="headerlink" title="While循环："></a>While循环：</h3><ul>
<li><p>while 条件：循环体 ——  条件为真则继续执行循环体</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">value=<span class="number">5</span></span><br><span class="line"><span class="keyword">while</span> value&gt;<span class="number">0</span>:</span><br><span class="line">    <span class="built_in">print</span>(value)</span><br><span class="line">    value-=<span class="number">1</span></span><br><span class="line"><span class="comment">#----------------</span></span><br><span class="line"><span class="number">5</span></span><br><span class="line"><span class="number">4</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="number">1</span></span><br></pre></td></tr></table></figure></li>
<li><p>Python首次运行while语句时，会先执行条件判断，但此时用户还没有输入，这回导致报错，所有必须要给用来做条件判断的变量一个初始值，即便是一个空字符串，但也是符合要求的</p>
</li>
<li><p>标志：在要求很多条件都满足才能继续运行的程序中，定义一个变量，用于判断整个程序是否处于活动状态，这个变量就是标志</p>
</li>
<li><p>break：使用break语句可以立即跳出当前循环，列表或字典的for循环均可使用</p>
</li>
<li><p>continue：使用continue语句会立即结束本次循环</p>
</li>
<li><p>不应该使用for循环在列表的遍历中进行修改，否则会导致Python难以跟踪其中的元素</p>
</li>
<li><p>可以使用while循环在遍历列表的同时进行修改</p>
</li>
<li><p>列表间移动元素：</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">unconfirmed_users = [<span class="string">&#x27;alice&#x27;</span>,<span class="string">&#x27;brian&#x27;</span>,<span class="string">&#x27;candace&#x27;</span>]</span><br><span class="line">confirmed_users = []</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> unconfirmed_users:</span><br><span class="line">    current_user = unconfirmed_users.pop()</span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;Verifying user：&quot;</span> + current_user.title())</span><br><span class="line">    confirmed_users.append(current_user)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;\nThe following users have been confirmed：&quot;</span>)</span><br><span class="line"><span class="keyword">for</span> confirmed_user <span class="keyword">in</span> confirmed_users:</span><br><span class="line">    <span class="built_in">print</span>(confirmed_user.title())</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">Verifying user：Candace</span></span><br><span class="line"><span class="string">Verifying user：Brian</span></span><br><span class="line"><span class="string">Verifying user：Alice</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">The following users have been confirmed：</span></span><br><span class="line"><span class="string">Candace</span></span><br><span class="line"><span class="string">Brian</span></span><br><span class="line"><span class="string">Alice</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h3><p>定义函数：</p>
<ul>
<li><p>def 关键字：定义函数名</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_19.png"></p>
</li>
<li><p>def 函数名（函数为完成其任务需要什么样的信息）：</p>
</li>
<li><p>紧跟其后的所有缩进行构成了函数体</p>
</li>
</ul>
<p>实参和形参</p>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_20.png"></p>
<ul>
<li>形参：username</li>
<li>实参：’ jesse ‘</li>
<li>调用函数时，将实参传递给函数，存储在形参中</li>
</ul>
<p>传递实参</p>
<ul>
<li><p>位置实参：实参的顺序与形参的顺序相同</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_21.png"></p>
<ul>
<li>位置实参的顺序很重要</li>
</ul>
</li>
<li><p>关键字实参：每个实参都有变量名和值组成</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_22.png"></p>
<ul>
<li>传递给函数名称-值对，直接在实参中将名称和值关联起来</li>
</ul>
</li>
<li><p>使用默认值时，在形参列表中必须先列出没有默认值的形参，再列出默认值的实参。</p>
</li>
<li><p>返回值：函数可以返回任何类型的值</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_23.png"></p>
<p>  返回字典</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_24.png"></p>
</li>
<li><p>让实参变为可选的：</p>
<ul>
<li>非默认实参（可选参数）必须放在默认实参（必填参数）后面</li>
</ul>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_25.png"></p>
</li>
<li><p>在函数中对列表进行的任何修改都是永久性的</p>
</li>
</ul>
<p>传递任意数量的实参</p>
<ul>
<li>Python允许函数从调用语句中收集任意数量的实参</li>
</ul>
<p><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_26.png"></p>
<ul>
<li><p><em>toppings：一个星号（</em>）让 Python 创建一个名为 toppings 的空元组，收到所有值都会封装到这个元组中，即便只收到一个值也是如此</p>
</li>
<li><p>如果要让函数接受不同类型的实参，必须在函数定义中，将接纳任意数量实参的形参放在最后</p>
</li>
<li><p>Python 先匹配 位置实参 和 关键字实参，再将余下的实参都收集到最后一个形参中</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_27.png"></p>
</li>
<li><p>****user_info：形参 **<strong>user_info 中的两个星号(</strong>)让 Python 创建一个名为 user_info 的空字典</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_28.png"></p>
</li>
</ul>
<p>将函数存储在模块中</p>
<ul>
<li><p>import 语句允许在当前运行的程序文件中使用模块中的代码</p>
</li>
<li><p>模块的扩展名为 .py 的文件</p>
</li>
<li><p>导入模块中特定函数的语法</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_29.png"></p>
<ul>
<li><p>from 模块名 import 函数名，函数名</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_30.png"></p>
</li>
<li><p>若是使用这种语法，调用函数时就无需使用点（ . ）进行引用</p>
</li>
</ul>
</li>
<li><p>为了防止导入的函数的名称冲突或太长，可以使用 as 指定别名</p>
<ul>
<li><p>from 模块名 import 函数名 as 别名</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_31.png"></p>
</li>
<li><p>from 模块名 as 别名</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_32.png"></p>
</li>
</ul>
</li>
<li><p>使用星号（ * ）运算符可以让 Python 导入模块中的所有函数</p>
<ul>
<li><p>from 模块名 import*</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_33.png"></p>
</li>
<li><p>无需使用点即可调用函数，尽量少用，容易出现重名、覆盖等问题</p>
</li>
</ul>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_34.png"></p>
</li>
</ul>
<h3 id="类"><a href="#类" class="headerlink" title="类"></a>类</h3><ul>
<li><p>首字母大写的名称指的是类</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_35.png"></p>
</li>
<li><p>方法 <strong>init</strong>( )：这是一个特殊的方法</p>
<ul>
<li>开头和末尾各有的两个下划线是一种约定，为了避免命名冲突</li>
<li>形参 self 是必不可少的，且必须位于其他形参前面</li>
<li>调用该方法时会自动传入实参 self ，它是指向实例本身的引用，让实例能够访问类中的属性和方法</li>
<li>self 会自动传递，所有我们不需要传递它，创建实例的时候只需要给其他形参提供值</li>
<li>通过实例访问的变量称为 <strong>属性</strong>： <a target="_blank" rel="noopener" href="http://self.name/">self.name</a> = name 获取存储在形参 name 中的值，将其存储到变量 name 中 ，然后该变量被关联到当前创建的实例。</li>
</ul>
</li>
<li><p>类中的每个属性都必须有初始值</p>
<ul>
<li><p>在方法__init()__内指定这种初始值是可行的</p>
</li>
<li><p>如果对某个属性这样做了，酒无需包含为它提供初始值的形参</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_36.png"></p>
</li>
</ul>
</li>
<li><p>修改属性的值有三种不同的方法</p>
<ul>
<li><p>直接通过实例进行修改</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_37.png"></p>
</li>
<li><p>通过方法进行设置</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_38.png"></p>
</li>
<li><p>通过方法进行递增（增加特定的值）</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_39.png"></p>
</li>
</ul>
</li>
<li><p>创建子类的实例时，Python需要先完成的任务是给父类的所有属性赋值</p>
</li>
<li><p>继承的方式</p>
<ul>
<li><p>父类必须包含在当前文件中，且位于子类前面</p>
</li>
<li><p>必须在括号内指定父类的名称</p>
</li>
<li><p>父类也成为 超类（superclass）</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_40.png"></p>
</li>
<li><p>Python2.7中的区别</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_41.png"></p>
</li>
</ul>
<h3 id="处理文件"><a href="#处理文件" class="headerlink" title="处理文件"></a>处理文件</h3><ul>
<li><p>从文件中读取数据</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_42.png"></p>
<ul>
<li>open（要打开的文件名）：Python 在当前执行的文件所在的目录中查找指定的文件，并返回一个表示文件的对象</li>
<li>关键字 with：在不需要访问文件后将其关闭；Python会在合适的时候自动关闭文件，所以最好不要过早的使用 close（）</li>
<li>read（）：读取文件并转换为字符串，在到达文件末尾的时候会返回一个空字符串</li>
</ul>
</li>
<li><p>文件路径</p>
<ul>
<li><p>相对文件路径</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_43.png"></p>
</li>
<li><p>绝对文件路径</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_44.png"></p>
</li>
</ul>
</li>
<li><p>逐行读取</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_45.png"></p>
<ul>
<li>readlines（）：从文件中读取每一行并存储在一个列表中</li>
<li>string[ start : end ]：取字符串从 start 到 end 位之间的字符</li>
<li>Python 的字符串if判断会自动帮你匹配你输入的字符串，例如在圆周率中查生日</li>
</ul>
</li>
<li><p>写入空文件</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_46.png"></p>
<ul>
<li><p>open（要打开的文件的名称，告诉Python我们要以写入模式打开这个软件）</p>
<ul>
<li><p>读取模式（ ‘ r ’ ），写入模式（ ‘ w ’ ），附加模式（ ‘ a ’ ）</p>
<p>  读取和写入模式（ ‘ r+ ’）</p>
</li>
<li><p>如果不写，默认是只读模式</p>
</li>
</ul>
</li>
<li><p>write（）：将一个字符串写入文件</p>
<ul>
<li>Python 只能将字符串写入文本文件</li>
<li>write 不会自动换行，所有需要我们自己添加换行符——“\n”</li>
</ul>
</li>
</ul>
</li>
<li><p>附加模式打开时，写入的文件都将添加到文件末尾，如果指定的文件不存在，Python 将会创建一个空文件</p>
</li>
</ul>
<h3 id="异常"><a href="#异常" class="headerlink" title="异常"></a>异常</h3><ul>
<li><p>异常时 Python 用来管理程序执行期间发生的错误的特殊对象</p>
</li>
<li><p>如果没有对异常进行处理，程序将停止，并显示一个 traceback ，其中包含有关异常的报告</p>
</li>
<li><p>使用 try - except 代码块处理异常</p>
</li>
<li><p>ZeroDivisionError 异常：当Python无法按你的要求做时，就会创建这种对象，这种情况下Python会停止运行并指出引发了哪种异常</p>
<ul>
<li><p>将可能发生异常的代码放在 try 内，将处理方式放在 except 内</p>
<p>  这样就不会因为异常而导致程序停止运行</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_47.png"></p>
</li>
<li><p>一些只需要在 try 中执行的代码可以放在 else中</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_48.png"></p>
</li>
</ul>
</li>
<li><p>FileNotFoundError 异常：找不到文件</p>
</li>
<li><p>pass：可以在代码块中使用它让 Python 什么都不要做，可以充当占位符，提醒你在程序的某个地方什么都没做</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_49.png"></p>
</li>
</ul>
<h3 id="Json模块（存储数据）"><a href="#Json模块（存储数据）" class="headerlink" title="Json模块（存储数据）"></a>Json模块（存储数据）</h3><ul>
<li><p>json.dump（）：接受两个实参，要存储的数据以及可用于存储数据的文件对象</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_50.png"></p>
</li>
<li><p>json.load（）：参数为需要读取数据的文件对象</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_51.png"></p>
</li>
<li><p>json.dumps（）：将字 典类 型转化为 字符串 类型</p>
</li>
<li><p>json.loads（）：将 字符串 类型转化成 字典 类型</p>
</li>
<li><p>json.dump（）：将 字典 类型转化为 字符串 类型</p>
</li>
<li><p>json.load（）：读取 Json 里的数据</p>
</li>
<li><p>重构：将代码划分为一系列完成具体工作的函数，这个过程就被称为 重构</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> json</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">get_stored_likeNum</span>():</span></span><br><span class="line">    <span class="string">&quot;&quot;&quot;如果存储了用户喜欢的数字，就获取它&quot;&quot;&quot;</span></span><br><span class="line">    filename = <span class="string">&#x27;likeNum.json&#x27;</span></span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        <span class="keyword">with</span> <span class="built_in">open</span>(filename) <span class="keyword">as</span> f_obj:</span><br><span class="line">            likeNum = json.load(f_obj)</span><br><span class="line">    <span class="keyword">except</span> FileNotFoundError:</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">None</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="keyword">return</span> likeNum</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">get_new_likeNum</span>():</span></span><br><span class="line">    <span class="string">&quot;&quot;&quot;提示用户输入喜欢的数字&quot;&quot;&quot;</span></span><br><span class="line">    likeNum = <span class="built_in">input</span>(<span class="string">&quot;What is your like number?&quot;</span>)</span><br><span class="line">    filename = <span class="string">&#x27;likeNum.json&#x27;</span></span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(filename, <span class="string">&#x27;w&#x27;</span>) <span class="keyword">as</span> f_obj:</span><br><span class="line">        json.dump(likeNum , f_obj)</span><br><span class="line">    <span class="keyword">return</span> likeNum</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">greet_num</span>():</span></span><br><span class="line">    likeNum = get_stored_likeNum()</span><br><span class="line">    <span class="keyword">if</span> likeNum:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;I know your favorite number! It&#x27;s：&quot;</span> + likeNum + <span class="string">&quot;!&quot;</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        likeNum = get_new_likeNum()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;We&#x27;ll remember you like number, &quot;</span> + likeNum + <span class="string">&quot;!&quot;</span>)</span><br><span class="line"></span><br><span class="line">greet_num()</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<h3 id="测试代码"><a href="#测试代码" class="headerlink" title="测试代码"></a>测试代码</h3><ul>
<li><p>测试函数</p>
<ul>
<li><p>单元测试和测试用例：</p>
<ul>
<li>Python 标准库中的模块 unittest 提供了代码测试工具。</li>
<li>单元测试：用于核实函数的某个方面没有问题</li>
<li>测试用例：是一组单元测试，这些单元测试一起核实函数在各种情况下的行为都符合要求</li>
<li>全覆盖式测试：用例包含一整套单元测试，涵盖了各种可能的函数使用方式</li>
</ul>
</li>
<li><p>可以通过的测试：</p>
<ul>
<li><p>先导入模块 unittest 以及要测试的函数，再创建一个继承unittest.TestCase 的类，并编写一系列对函数不同行为的不同方面进行测试</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_52.png"></p>
</li>
<li><p>在①处，创建了一个类，并且继承了 unittest.TestCase</p>
</li>
<li><p>在运行这个脚本的时候，所有以 test_ 打头的方法都将自动运行</p>
</li>
<li><p>unittest 类最有用的功能之一：一个断言方法（ assertEqual（））</p>
<ul>
<li>这个方法就是将测试的结果与预期的结果放入其中，如果相同则成功</li>
</ul>
</li>
<li><p>代码 unittest.main（） 让Python运行这个文件中的测试</p>
</li>
</ul>
</li>
<li><p>不能通过的测试：</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_53.png"></p>
</li>
</ul>
</li>
<li><p>测试类</p>
<ul>
<li><p>各种断言方法</p>
<p>  <img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/PythonNotion_54.png"></p>
</li>
<li><p>方法 setUp（）</p>
<ul>
<li>如果你在 TestCase 类中包含了方法 setUp ( ) ，Python 将先运行它，再运行各个以 test_ 打头的方法</li>
</ul>
</li>
</ul>
</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">AikikoXHJ</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2021/06/16/PythonNotion/">http://example.com/2021/06/16/PythonNotion/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">Aikikoの小窝</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Python/">Python</a></div><div class="post_share"><div class="social-share" data-image="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/background.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="next-post pull-full"><a href="/2021/06/06/GitMarkdown/"><img class="next-cover" src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/background.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Git 笔记</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/XHJ.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">AikikoXHJ</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">2</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">2</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/AikikoXHJ"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/AikikoXHJ" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:AikikoXHJ@outlook.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">傻傻的脑袋里有大大的梦想~</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E6%93%8D%E4%BD%9C"><span class="toc-number">1.</span> <span class="toc-text">字符操作</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E5%85%B8%EF%BC%9A"><span class="toc-number">2.</span> <span class="toc-text">字典：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%94%A8%E6%88%B7%E8%BE%93%E5%85%A5%E4%B8%8E%E6%B1%82%E6%A8%A1%E8%BF%90%E7%AE%97"><span class="toc-number">3.</span> <span class="toc-text">用户输入与求模运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#While%E5%BE%AA%E7%8E%AF%EF%BC%9A"><span class="toc-number">4.</span> <span class="toc-text">While循环：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0"><span class="toc-number">5.</span> <span class="toc-text">函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB"><span class="toc-number">6.</span> <span class="toc-text">类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%84%E7%90%86%E6%96%87%E4%BB%B6"><span class="toc-number">7.</span> <span class="toc-text">处理文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%82%E5%B8%B8"><span class="toc-number">8.</span> <span class="toc-text">异常</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Json%E6%A8%A1%E5%9D%97%EF%BC%88%E5%AD%98%E5%82%A8%E6%95%B0%E6%8D%AE%EF%BC%89"><span class="toc-number">9.</span> <span class="toc-text">Json模块（存储数据）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E4%BB%A3%E7%A0%81"><span class="toc-number">10.</span> <span class="toc-text">测试代码</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/06/16/PythonNotion/" title="Python 笔记"><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/background.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Python 笔记"/></a><div class="content"><a class="title" href="/2021/06/16/PythonNotion/" title="Python 笔记">Python 笔记</a><time datetime="2021-06-16T06:57:52.000Z" title="发表于 2021-06-16 14:57:52">2021-06-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/06/06/GitMarkdown/" title="Git 笔记"><img src="https://cdn.jsdelivr.net/gh/AikikoXHJ/cloudimg/img/background.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Git 笔记"/></a><div class="content"><a class="title" href="/2021/06/06/GitMarkdown/" title="Git 笔记">Git 笔记</a><time datetime="2021-06-05T16:00:00.000Z" title="发表于 2021-06-06 00:00:00">2021-06-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By AikikoXHJ</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script async src = "/js/FunnyTitle.js"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>